package com.home.duy.code.components;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.home.Common.Common;

public class ImagePanel extends JPanel
{
	/*the default image to use*/
	String imageFile = "/images/background.png";

	public ImagePanel()
	{
		super();
	}

	public ImagePanel(String image)
	{
		super();
		this.imageFile = image;
	}

	public ImagePanel(LayoutManager layout)
	{
		super(layout);
	}

	public void paintComponent(Graphics g)
	{
		BufferedImage img = null;
		/*create image icon to get image*/
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		URL url = loader.getResource(imageFile);
		try {
			
			img = ImageIO.read(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		ImageIcon imgIcon = Common.convertBufferImageToImageIcon(img);
		Image image = imgIcon.getImage();

		/*Draw image on the panel*/
		super.paintComponent(g);

		if (image != null)
			g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
	}
}
